function audioComponent(options) {
    // 默认参数设置
    this.src = options.src || "";
    this.autoplay = options.autoplay || false;
    this.loop = options.loop || true;
    this.btnPlay = options.btnPlay || "";
    this.btnPause = options.btnPause || "";
    this.slideBar = options.slideBar || "";
    this.slideThumb = options.slideThumb || "";
    this.playing = options.playing || null;
    this.ended = options.ended || null;
    this.seeked = options.seeked || null;
    this.duration = 0;
    this.currentTime = 0;
    this.isSlideMouseMove = false;
};
audioComponent.prototype = {
    // 创建audio元素
    _create: function() {
        var _this = this;
        var audio = $("<audio>").attr(
            {
                "src": _this.src,
                "autoplay": _this.autoplay,
                "loop": _this.loop
            });
        $("body").append(audio);
        _this.audio = audio[0];
    },
    // 获取音频播放的总时长
    _seeked: function() {
        var _this = this;
        $(_this.audio).on('seeked progress', function() {
            _this.duration = _this.audio.seekable.end(0);
            if(typeof _this.seeked == "function") {
                _this.seeked(_this.duration);
            }
        });
    },
    // 播放
    play: function(callback) {
        var audio = this.audio;
        audio.play();
        if(typeof callback == "function") {
            callback(audio.currentTime);
        }
    },
    // 暂停
    pause: function(callback) {
        var audio = this.audio;
        audio.pause();
        if(typeof callback == "function") {
            callback(audio.currentTime);
        }
    },
    // 播放与暂停切换
    playPause: function(callback) {
        var audio = this.audio;
        var isPause = audio.paused;
        if(isPause) {
            this.play();
        } else {
            this.pause();
        }
        if(typeof callback == "function") {
            callback(audio.currentTime, isPause);
        }
    },
    // 获取音频总播放时间
    getDuration: function() {
        return this.duration;
    },
    // 获取音频当前播放时间
    getCurrentTime: function() {
        return this.currentTime;
    },
    // 音频正在播放的回调函数
    onPlaying: function(callback) {
        var _this = this,
            audio = this.audio;
        this.currentTime = audio.currentTime;
        if(audio.addEventListener) {
            audio.addEventListener("timeupdate", function() {
                if(typeof callback == "function") {
                    callback(audio.currentTime);
                }
                if(typeof _this.playing == "function") {
                    _this.playing(audio.currentTime);
                }
            });
        }
    },
    // 按钮的事件绑定
    _domBind: function() {
        var _this = this;
        if(this.btnPlay) {
            $(document).on("touchstart", this.btnPlay, function(e) {
                console.log($(_this.btnPlay))
                $(_this.btnPause).removeClass('hidden')
                $(_this.btnPlay).addClass('hidden')
                _this.play();
                e.preventDefault()

            });

            $(document).on("click", this.btnPlay, function(e) {
                console.log($(_this.btnPlay))
                $(_this.btnPause).removeClass('hidden')
                $(_this.btnPlay).addClass('hidden')
                _this.play();
                e.preventDefault()

            });
        }
        if(this.btnPause) {
            $(document).on("touchstart", this.btnPause, function(e) {
                console.log($(_this.btnPlay))
                $(_this.btnPlay).removeClass('hidden')
                $(_this.btnPause).addClass('hidden')
                _this.pause();
                e.preventDefault()
            });
            $(document).on("click", this.btnPause, function(e) {
                console.log($(_this.btnPlay))
                $(_this.btnPlay).removeClass('hidden')
                $(_this.btnPause).addClass('hidden')
                _this.pause();
                e.preventDefault()
            });
        }
    },
    // 滑动条事件绑定
    _slideBind: function() {
        var _this = this,
            slideBar = $(this.slideBar),
            slideThumb = $(this.slideThumb),
            audio = this.audio;
        var totalWidth = slideBar.width();
        // 按钮跟随音乐播放
        _this.onPlaying(function(curTime) {
            if(!_this.isSlideMouseMove) {
                var duration = _this.duration;
                ratio = totalWidth / duration;
                slideThumb.css("left", curTime * ratio);
            }
        });
        // 滑块滑动事件绑定
        var isMousedown = false,
            initConfig = {
                time: 0
            },
            moveConfig = {};
        $(document).on("mousedown", this.slideThumb, function(e) {
            isMousedown = true;
            initConfig.time = audio.currentTime;
            initConfig.x = e.pageX;
            initConfig.left = parseInt($(this).css("left"));
            _this.isSlideMouseMove = true;
        })
            .on("mousemove", this.slideThumb, function(e) {
                if(isMousedown) {
                    e.preventDefault();
                    moveConfig.x = e.pageX;
                    var deltaX = moveConfig.x - initConfig.x;
                    var slideLeft = initConfig.left + deltaX;
                    if(slideLeft >= totalWidth) {
                        slideLeft = totalWidth;
                    } else if(slideLeft <= 0) {
                        slideLeft = 0;
                    }
                    moveConfig.time = slideLeft * _this.duration / totalWidth;
                    slideThumb.css("left", slideLeft);
                }
            })
            .on("mouseup", function(e) {
                if(isMousedown) {
                    isMousedown = false;
                    audio.currentTime = moveConfig.time;
                    audio.play();

                    moveConfig = {};
                    initConfig = {};

                    setTimeout(function() {
                        _this.isSlideMouseMove = false;
                    }, 100);
                }
            });

        // 滑动条上的点击事件
        $(document).on("click", this.slideBar, function(e) {
            e.stopPropagation();
            var mouseX = e.pageX;
            var offsetLeft = $(this).offset().left;
            var slideLeft = mouseX - offsetLeft;
            var currentTime = slideLeft * _this.duration / totalWidth;
            slideThumb.css("left", slideLeft);
            audio.currentTime = currentTime;
            audio.play();
        });
    },
    init: function() {
        this._create();
        this._seeked();
        this._domBind();
        this._slideBind();
    }
};
audioComponent.create = function(options) {
    var audio = new audioComponent(options);
    audio.init();
    return audio;
};




function timeFormat(s) {
    var second = s % 60 > 0 ? parseInt(s % 60) : s;
    var minute = s / 60 > 0 ? parseInt(s / 60) : 0;
    second = second >= 10 ? second : "0" + second;
    minute = minute >= 10 ? minute : "0" + minute;
    return minute + ":" + second;
}
